#include "video.h"


void draw_line(unsigned int width, unsigned int high,
				 unsigned int x, unsigned int y)
{
	lcd_fill(x-LINE_W, y-LINE_W, x+width+LINE_W, y, PIC_LINE_COLOR);
	lcd_fill(x-LINE_W, y, x, y+high, PIC_LINE_COLOR);
	lcd_fill(x+width, y, x+LINE_W+width, y+high, PIC_LINE_COLOR);
	lcd_fill(x-LINE_W, y+high, x+width+LINE_W, y+LINE_W+high, PIC_LINE_COLOR);
}


void play_video(void *arg)
{
	char cmd_buf[64]={0};
	sprintf(cmd_buf,"ffplay -autoexit -fs %s%s &\n",VIDEO_PATH,(char *)arg);
	system(cmd_buf);
}

void stop_video(void)
{
	system("killall ffplay \n");
}


void flash_background()
{
	lcd_draw_jpg(0,0,VIDEO_BACKGROUND,NULL,0,0);
}

void video_list()
{
	struct coordinate coor;
	bool released = false;
	int flag_choose = 0;
	bool start = false;
	char video_name[32];
	flash_background();
	
	while(1)
	{
		released = true;
		ts_trace(ts_fd, &coor, &released);
		if(!start)
		{
			if(coor.x>120 && coor.x<240 && coor.y>120 && coor.y<280 && released)
			{
				flash_background();
				draw_line(120,160,127,123);
				flag_choose =1;
			}
			if(coor.x>330 && coor.x<450 && coor.y>120 && coor.y<280 && released)
			{
				flash_background();
				draw_line(120,160,336,123);
				flag_choose =2;
			}
			if(coor.x>550 && coor.x<670 && coor.y>120 && coor.y<280&& released)
			{
				flash_background();
				draw_line(120,160,551,123);
				flag_choose =3;
			}
			if(coor.x>250 && coor.x<350 && coor.y>420 && coor.y<480 && released)
			{
				if(flag_choose != 0)
				{	switch(flag_choose)
					{
							case 1:	
								play_video("tom.avi");
								load_join(40);
								start = true;
								break;
							case 2:	
								play_video("beer.avi");					
								start = true;
								load_join(40);
								break;
							case 3:		
								play_video("Mac.avi");
								load_join(40);
								start = true;
								break;
							default:
								break;			
					}
				}			
			}
			if(coor.x>420 && coor.x<520 && coor.y>420 && coor.y<480 && released)
			{
				return;
			}
		}
		if(start)
		{
			ts_trace(ts_fd, &coor, &released);
			if(coor.x>700 && coor.x<800 && coor.y>0 && coor.y<100 && released)
			{
				printf("exit play!\n");
				stop_video();
				flash_background();
				flag_choose =0;
				start = false;
			}
		}	
	}
}